﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
 
using System.Text;
using System.Windows.Forms;
using FarPoint.Win.Spread;
using FarPoint.Win.Spread.Model;
using FarPoint.Win.Spread.CellType;

namespace Demo
{
    public partial class columnGroupFooter : DemoBaseUserControl
    {
        public columnGroupFooter()
        {
            InitializeComponent();
            this.Title = "分组和多行列脚";
            this.Description = "Spread .NET  可在一个表单中设置“页脚”和“分组页脚”，可在“页脚”和“分组页脚”上设置信息比如一些“公式”或“文字”。页脚在表单底部。如果设置了分组功能，那么“分组页脚”在每个分组的最后一行。";
        }
        private void columnGroupFooter_Load_1(object sender, EventArgs e)
        {
            this.FpSpread1.Sheets.Count = 1;
            FpSpread1.AllowColumnMove = true;
            FpSpread1.Sheets[0].SheetName = "ColumnGroupFooter";
            FpSpread1.Sheets[0].AllowGroup = true;
            FpSpread1.Sheets[0].GroupBarInfo.Visible = true;
            CreateDataForColumnFooter();
            SettingForDefaultGroupFooter();
        }
        private void SettingForDefaultGroupFooter()
        {
            SheetView sv1 = FpSpread1.ActiveSheet;
            sv1.AllowGroup = true;
            GroupDataModel gdm = new GroupDataModel(sv1.Models.Data);
            sv1.Models.Data = gdm;
            SortInfo[] siList = new SortInfo[] { new SortInfo(2, true) };
            gdm.Group(siList, null);

            DefaultGroupFooter dgf = FpSpread1.Sheets[0].DefaultGroupFooter[2];
            ISheetDataModel dataModel = dgf.DataModel;
            FarPoint.Win.Spread.CellType.TextCellType a = new TextCellType();
            this.FpSpread1_Sheet1.Columns.Get(0).CellType = a;
            (dataModel as IAggregationSupport).SetCellAggregationType(0, 3, AggregationType.Count);
            (dataModel as IAggregationSupport).SetCellAggregationFormat(0, 3, "{0}");
            (dataModel as IAggregationSupport).SetCellAggregationType(0, 4, AggregationType.Sum);
            (dataModel as IAggregationSupport).SetCellAggregationFormat(0, 4, "{0}");
            FpSpread1.ActiveSheet.GroupFooterVisible = true;

        }
        private void CreateDataForColumnFooter()
        {
            FpSpread1.DataSource = 销售明细表TableAdapter.GetData();

            FpSpread1.ActiveSheet.ColumnFooter.Visible = true;
            FpSpread1.ActiveSheet.ColumnFooter.RowCount = 2;
            FpSpread1.ActiveSheet.ColumnFooter.SetAggregationType(0, 6, FarPoint.Win.Spread.Model.AggregationType.Sum);
            FpSpread1.ActiveSheet.ColumnFooter.SetAggregationFormat(0, 6, "总计: {0}");

            FpSpread1.ActiveSheet.ColumnFooter.SetAggregationType(0, 3, FarPoint.Win.Spread.Model.AggregationType.Max);
            FpSpread1.ActiveSheet.ColumnFooter.SetAggregationFormat(0, 3, "最大: {0}");
            FpSpread1.ActiveSheet.ColumnFooter.SetAggregationType(1, 3, FarPoint.Win.Spread.Model.AggregationType.Min);
            FpSpread1.ActiveSheet.ColumnFooter.SetAggregationFormat(1, 3, "最小: {0}");

            FpSpread1.ActiveSheet.Columns[2, 3].Width = 100;
            FpSpread1.ActiveSheet.Columns[5, 6].Width = 100;

            FpSpread1.ActiveSheet.ColumnFooter.Cells[0, 0].ColumnSpan = 2;
            FpSpread1.ActiveSheet.ColumnFooter.Cells[0, 0].RowSpan = 2;
            FpSpread1.ActiveSheet.ColumnFooter.Cells[0, 0].Text = "概要";
            FpSpread1.ActiveSheet.ColumnFooter.Rows[0].Height = 20;
            FpSpread1.ActiveSheet.ColumnFooter.Columns[5].Width = 100;
            FpSpread1.ActiveSheet.ColumnFooter.Columns[7].Width = 120;
        }
    }
}